/* Wrapper.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the Wrapper class, which can calculate line breaks for text wrapping
starting at any arbitrary position in a text object, which potentially may contain
a large amount of text.
*/

#ifndef __MZ_WRAPPER_H__
#define __MZ_WRAPPER_H__

#include "text.h"
#include "ViewProperties.h"

#include <vector>

using std::vector;



class Wrapper {
public:
	Wrapper(const ViewProperties<text> &properties);
	
	// Calculate line breaks in order to position the given cursor iterator
	// on the given line of the display.
	// If the cursor line is less than zero, wrap to that number of lines
	// above the bottom of the view (so, -1 is the last visible line).
	void Wrap(const text &t, text::const_iterator cursor, int cursorLine);
	
	// Get the start of the given line of the display.
	// The end of each line is the start of the next.
	// The given line must be <= viewProperties.lineCount.
	// If the given line is negative, this returns the beginning of the line
	// that number of entries up from the bottom.
	text::const_iterator operator[](int line) const;
	
	// Get the line index of the given iterator.
	int LineOf(text::const_iterator it) const;
	// Get the position, within its line, of the given iterator.
	int PositionOf(text::const_iterator it) const;
	// Get the position of the given iterator within the given line.
	// This result is only valid if the iterator is, indeed, on that line.
	int PositionOf(text::const_iterator it, int line) const;
	// Map a line index, which may be relative to the bottom, to an
	// absolute line index.
	int AbsoluteLine(int line) const;
	
	
private:
	const ViewProperties<text> &viewProperties;
	
	vector<text::const_iterator> lines;
};



#endif



/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */

